const Base = require('./base.js');
const rq = require('request');

module.exports = class extends Base {
  async indexAction() {
    return this.display();
  }
  /**
   * 添加网址
   */
  async addAction() {
    let data = this.post();
    if (think.isEmpty(data) || !data.web_name || !data.web_user || !data.web_pwd || !data.web_url) {
      return this.json({code: 1000, data: null, msg: '数据格式错误'});
    }
    const modelWeb = this.model('web');

    if (!think.isEmpty(await modelWeb.where({web_url: data.web_url}).find())) {
      return this.json({code: 1000, data: null, msg: '网址已添加'});
    }
    let res = await modelWeb.addData(data);
    if (think.isEmpty(res)) {
      return this.json({code: 1000, data: null, msg: '添加失败'});
    } else {
      return this.json({code: 0, data: res, msg: '添加成功'});
    }
  }

  
  /**
   * 添加网址
   */
  async addsourceAction() {
    let data = this.post();
    if (think.isEmpty(data) || !data.source_name || !data.source_aurl) {
      return this.fail('数据格式错误');
    }
    const modelSource = this.model('source');

    let res = await modelSource.addData(data);
    if (think.isEmpty(res)) {
      return this.fail('source_id' in data ? '修改失败' : '添加失败');
    } else {
      return this.success(res, 'source_id' in data ? '修改成功' : '添加成功');
    }
  }

  /**
   * 查询网站列表
   */
  async listAction() {
    let {pageIndex = 1, pageSize = 20} = this.get();
    const modelWeb = this.model('web');
    let res = await modelWeb.page(pageIndex, pageSize).countSelect();
    if (!think.isEmpty(res)) {
      return this.success(res, '查询成功');
    } else {
      return this.fail('查询失败');
    }
  }


  /**
   * 查询网站列表
   */
  async sourceAction() {
    let {pageIndex = 1, pageSize = 20} = this.get();
    const modelSource = this.model('source');
    let res = await modelSource.page(pageIndex, pageSize).countSelect();
    if (!think.isEmpty(res)) {
      return this.success(res, '查询成功');
    } else {
      return this.fail('查询失败');
    }
  }



  async addloginAction() {
    // let data = this.post();
    // if (think.isEmpty(data) || !data.web_name || !data.web_user || !data.web_pwd || !data.web_url) {
    //   return this.json({code: 1000, data: null, msg: '数据格式错误'});
    // }
    // let loginUrl = data.web_url;
    let loginUrl = 'http://www.jhdosing.com/jhadm/login.php';
    let html = await rp(loginUrl);
    return this.success(html);
    
  }
};
